﻿using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MarkDownFileSplitter.Meilisearch
{
    [ToolboxItem(false)]
    public partial class FilterSettingControl : UserControl
    {
        public string CurrentIndexUid { get; set; }

        public FilterSettingControl()
        {
            InitializeComponent();
        }

        public async Task RefreshGridAsync(List<string> attributeNames)
        {
            JObject allSettting = await MeilisearchUploader.Instance.GetIndexSettings(CurrentIndexUid);
            this.textBox1.Text = allSettting.ToString();

            JArray filterableAttributes = await MeilisearchUploader.Instance.GetSettingOfFilterableAttributes(CurrentIndexUid);
            dataGridView_setting.Rows.Clear();
            //获取所有字段名，以及设置过的字段信息
            foreach (string attrName in attributeNames)
            {
                FilterAttribute? filterAttribute = GetFilterAttribute(attrName, filterableAttributes);
                AddRow(attrName, filterAttribute);
            }
        }

        private FilterAttribute? GetFilterAttribute(string attrName, JArray filterableAttributes)
        {
            foreach (var filterableAttribute in filterableAttributes)
            {
                bool inIt = false;
                foreach (var p in (JArray)filterableAttribute["attributePatterns"])
                {
                    if (attrName == p.ToString())
                    {
                        inIt = true;
                        break;
                    }
                }
                if (inIt)
                {
                    var filter = filterableAttribute["features"]["filter"];
                    bool equality = filter["equality"].Value<bool>();
                    bool comparison = filter["comparison"].Value<bool>();
                    return new FilterAttribute(attrName, equality, comparison);
                }
            }
            return null;
        }

        private void AddRow(string attrName, FilterAttribute? filterAttribute)
        {
            DataGridViewRow row = new DataGridViewRow();

            // 创建第一列的CheckBox单元格
            DataGridViewCheckBoxCell filterableCell = new DataGridViewCheckBoxCell();
            filterableCell.Value = filterAttribute != null;
            filterableCell.ReadOnly = false;
            row.Cells.Add(filterableCell);

            // 创建第二列的文本单元格
            DataGridViewTextBoxCell attrNameCell = new DataGridViewTextBoxCell();
            attrNameCell.Value = attrName;
            row.Cells.Add(attrNameCell);

            // 创建第三列的CheckBox单元格
            DataGridViewCheckBoxCell equalityCell = new DataGridViewCheckBoxCell();
            equalityCell.Value = filterAttribute == null ? false : filterAttribute.EnableEquality;
            row.Cells.Add(equalityCell);

            // 创建第四列的CheckBox单元格
            DataGridViewCheckBoxCell comparisonCell = new DataGridViewCheckBoxCell();
            comparisonCell.Value = filterAttribute == null ? false : filterAttribute.EnableComparison;
            row.Cells.Add(comparisonCell);

            dataGridView_setting.Rows.Add(row);
        }

        private async void button_upload_Click(object sender, EventArgs e)
        {
            // 从DataGridView中获取过滤器设置
            List<FilterAttribute> filterAttributes = new List<FilterAttribute>();
            foreach (DataGridViewRow row in dataGridView_setting.Rows)
            {
                if ((bool)row.Cells[0].Value == true)
                {
                    filterAttributes.Add(new FilterAttribute(row.Cells[1].Value.ToString(), Convert.ToBoolean(row.Cells[2].Value), Convert.ToBoolean(row.Cells[3].Value)));
                }
            }

            if (CurrentIndexUid == null)
                return;

            var result = await MeilisearchUploader.Instance.UpdateFilterSetting(CurrentIndexUid, filterAttributes);
            await MeilisearchUploader.Instance.IsTaskSuccess(result["taskUid"].ToString());
        }

        private void toolStripButtonUpdateParmaryKey_Click(object sender, EventArgs e)
        {

        }
    }
}
